//
//  BCUserOverdueController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/14.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserOverdueController.h"

@interface BCUserOverdueController ()

@property (nonatomic, strong) QMUILabel *errLabel;

/**
 *sureButton
 */
@property (nonatomic, strong) QMUIButton *sureButton;

/**
 *reLoginButton
 */
@property (nonatomic, strong) QMUIButton *reLoginButton;

#pragma mark -

/**
 *error
 */
@property (nonatomic, copy) NSError *error;

/**
 *BCUserOverdueCompleteBlcok
 */
@property (nonatomic, copy) BCUserOverdueCompleteBlcok completeBlock;

@end

@implementation BCUserOverdueController

+ (void)showOverdureWithError:(NSError *)error complete:(BCUserOverdueCompleteBlcok)complete {
    UserOverdueWindow *window = [[UserOverdueWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window.windowLevel = UIWindowLevelAlert;
    //vc
    BCUserOverdueController *vc = [BCUserOverdueController new];
    vc.showWindow = window;
    vc.error = error;
    vc.completeBlock = complete;
    window.rootViewController = vc;
    [window makeKeyAndVisible];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
}
- (void)setupSubviews {
    [self.view addSubview:self.backgroundView];
    self.backgroundView.alpha = 0.2;
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    //
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(-Height_Bottom_SafeArea);
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
        make.height.mas_equalTo(0);
    }];
    self.contentView.layer.cornerRadius = 5;
    self.contentView.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.14].CGColor;
    self.contentView.layer.shadowRadius = 5;
    self.contentView.layer.shadowOffset = CGSizeMake(0, -1);
    self.contentView.layer.shadowOpacity = 1;
    //
    [self setContents];
    [self.view layoutIfNeeded];
}
- (void)setContents {
    //
    CGFloat contenHeight = 0;
    //
    self.errLabel = [QMUILabel new];
    self.errLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.errLabel.textColor = [UIColor blackColor];
    self.errLabel.numberOfLines = 0;
    self.errLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.errLabel];
    [self.errLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(20.0);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_lessThanOrEqualTo(self.contentView).offset(-40.0);
    }];
    self.errLabel.text = self.error!=nil?self.error.domain:@"Login Error";
    //
    self.sureButton = [QMUIButton new];
    [self.sureButton setTitle:BCLocalizedKey(@"Sure") forState:UIControlStateNormal];
    [self.contentView addSubview:self.sureButton];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.errLabel.mas_bottom).offset(20.0);
        make.left.mas_equalTo(self.contentView);
        make.height.mas_equalTo(44.0);
        make.width.mas_equalTo(self.contentView).multipliedBy(.5);
    }];
    [self.sureButton addTarget:self action:@selector(sureButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.reLoginButton = [QMUIButton new];
    [self.reLoginButton setTitle:BCLocalizedKey(@"Login Again") forState:UIControlStateNormal];
    [self.contentView addSubview:self.reLoginButton];
    [self.reLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).multipliedBy(.5);
        make.height.mas_equalTo(self.sureButton);
        make.top.mas_equalTo(self.sureButton);
    }];
    [self.reLoginButton addTarget:self action:@selector(reLoginButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    UIView *hLine = [UIView new];
    hLine.backgroundColor = [UIColor separatorColor];
    [self.contentView addSubview:hLine];
    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sureButton);
        make.left.right.mas_equalTo(self.contentView);
        make.height.mas_lessThanOrEqualTo(1.0);
    }];
    UIView *vline = [UIView new];
    vline.backgroundColor = hLine.backgroundColor;
    [self.contentView addSubview:vline];
    [vline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sureButton);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(1);
        make.bottom.mas_equalTo(self.sureButton);
    }];
    //
    [self.contentView layoutIfNeeded];
    contenHeight += 20.0;
    contenHeight += CGRectGetHeight(self.errLabel.frame);
    contenHeight += 20.0;
    contenHeight += 44.0;
    //
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contenHeight);
    }];
}

#pragma mark - sureButtonDidClickedAction
- (void)sureButtonDidClickedAction {
    if (self.completeBlock) {
        self.completeBlock(true, false);
    }
    [self dismissAction];
}
#pragma mark - reLoginButtonDidClickedAction
- (void)reLoginButtonDidClickedAction {
    if (self.completeBlock) {
        self.completeBlock(false, true);
    }
    [self dismissAction];
}



- (void)dismissAction {
    [self dismissWindow];
}

@end
